<template>
  <div>
    <el-tooltip class="box-item" effect="dark" :content="btnText === ('' || undefined) ? '选择全宗' : btnText" placement="top">
      <el-button text @click="getFondsData">全宗</el-button>
    </el-tooltip>
    <!-- 弹出框 -->
    <el-dialog v-model="dialogSetting.visible" :title="dialogSetting.title" width="30%" draggable>
      <ztree :setting="treeSetting.setting" :nodes="treeSetting.treeData" @onClick="onClick"></ztree>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogSetting.visible = false">取消</el-button>
          <el-button type="primary" @click="handle"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import { useDynamicStore } from "@/stores/modules/dynamic";
import { useFondsStore } from "@/stores/modules/fonds";
import { useRouter } from "vue-router";
import localCache from "@/utils/cache";

const dynamicStore = useDynamicStore();
const fondsStore = useFondsStore();
const { proxy } = useCurrentInstance();
const router = useRouter();
// 按钮显示文字
let btnText = ref();
// tree配置
const treeSetting = reactive({
  setting: {
    // ztree配置
    data: {
      simpleData: {
        enable: true,
        idKey: "ID",
        pIdKey: "FatherID"
      },
      key: {
        name: "Name"
      }
    },
    view: {
      showIcon: true
    }
  },
  treeData: [] as any[],
  ztreeObj: {} as any
});
// 弹出框配置
const dialogSetting = reactive({
  visible: false,
  title: ""
});
// 父级数据
const fatherData = ref();
// 获取全宗数据
const getFondsData = () => {
  dialogSetting.visible = true;
  dialogSetting.title = "选择全宗";
  treeSetting.treeData = fondsStore.fondsData.length > 0 ? fondsStore.fondsData : fondsStore.getFondsData;
  treeSetting.treeData.forEach(item => {
    if (item.Type === "") {
      item.iconOpen = new URL("@/assets/imgs/opened_folder.png", import.meta.url).href;
      item.iconClose = new URL("@/assets/imgs/folder.png", import.meta.url).href;
    }
    if (item.Type === "Fonds") {
      item.iconClose = "data:image/jpeg;base64," + item.Fonds_Icon1;
      item.iconOpen = "data:image/jpeg;base64," + item.Fonds_Icon2;
    }
    if (item.Type === "FM") {
      item.iconClose = "data:image/jpeg;base64," + item.FM_Icon1;
      item.iconOpen = "data:image/jpeg;base64," + item.FM_Icon2;
    }
    if (item.Type === "FMT") {
      if (item.FMT_Icon1 && item.FMT_Icon2) {
        item.iconClose = "data:image/jpeg;base64," + item.FMT_Icon1;
        item.iconOpen = "data:image/jpeg;base64," + item.FMT_Icon2;
      } else {
        item.icon = new URL("@/assets/imgs/file.png", import.meta.url).href;
      }
    }
  });
};
// 点击树节点
const onClick = (treeNode: any) => {
  // proxy.$showLoading();
  if (treeNode.Type === "FMT") {
    treeSetting.ztreeObj = treeNode;
    getFatherFmtId(treeNode);
  } else {
    treeSetting.ztreeObj = {};
    proxy.$message.warning("请选择FMT节点数据");
  }
};
// 根据id查找父级
const getParent = (treeNode: any, parentNameList = [] as string[]) => {
  if (treeNode.FatherID) {
    const patherNode = treeSetting.treeData.find(v => v.ID === treeNode.FatherID);
    parentNameList.push(patherNode.Name);
    getParent(patherNode, parentNameList);
  }
  return parentNameList;
};
// 获取当前选中树节点的父级Fmtid
const getFatherFmtId = (treeNode: any) => {
  proxy.$vueAxios
    .post(
      "Master_Base_FormModel_Table/GetConditional",
      {
        order: "FMT_Type ASC",
        dtoList: [
          {
            fieldName: "FM_ID",
            fieldValue: treeNode.FM_ID
          }
        ]
      },
      {
        noLoading: true
      }
    )
    .then((res: any) => {
      fatherData.value = res.List.filter((v: any) => (v.FMT_ID = treeNode.FMT_Parent_ID))[0];
    });
};
// 选择全宗确认事件
const handle = () => {
  if (treeSetting.ztreeObj.Type === "FMT") {
    btnText.value = "";
    dialogSetting.visible = false;
    const nameString = getParent(treeSetting.ztreeObj).reverse().join("/");
    btnText.value = nameString + "/" + treeSetting.ztreeObj.Name;
    fondsStore.SetFondsObj(treeSetting.ztreeObj);
    localCache.setCache("fondObj", treeSetting.ztreeObj, false);
    router.push({
      // path: "/system/dynamicMain/" + treeSetting.ztreeObj.ID,
      name: "MainModal",
      params: { id: treeSetting.ztreeObj.ID },
      query: {
        fmid: treeSetting.ztreeObj.FM_ID,
        fmtid: treeSetting.ztreeObj.FMT_ID,
        status: treeSetting.ztreeObj.Archive_Status,
        childrenFmtId: treeSetting.ztreeObj.children ? treeSetting.ztreeObj.children.map((v: any) => v.FMT_ID) : "",
        fatherObj: undefined,
        fatherFmtId: fatherData.value ? fatherData.value.FMT_ID : undefined,
        fondsId: treeSetting.ztreeObj.Fonds_ID
      }
    });
  } else {
    proxy.$message.warning("请选择FMT节点数据");
  }
};
</script>

<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 300px;
}
</style>
